#include"logger.h"

Logger *Logger::self = NULL;
int Logger::refcount = 0;

Logger::Logger(){
	out.open("out.txt", std::ios::binary);
	level = LOG_ERROR;
	buff = "";
}

Logger::~Logger(){
	out<<buff;
	out.close();
}

void Logger::send(int lvl, std::string message){
	time_t t;
	time(&t);
	std::ostringstream stream;
	stream<<ctime(&t)<<" : "<<message<<"\n";
	message = stream.str();
	if(level == LOG_INF){
		if(lvl != LOG_ERROR){
			if(buff.length() + message.length() < max_bsize){
				buff += message;
			}else{
				out<<buff;
				buff = message;
			}
		}else{
			out<<buff<<message;
			exit(lvl);
		}
	}
	if(level == LOG_ERROR){
		if(lvl == LOG_ERROR){
			out<<buff<<message;
			exit(100);
		}
	}
}

void Logger::sendERROR(int last_error, std::string message){
	time_t t;
	time(&t);
	std::ostringstream stream;
	stream<<ctime(&t)<<" : "<<"Last error code = "<<last_error<<" , "<<message<<"\n";
	message = stream.str();
	out<<buff<<message;
	exit(last_error);
}

void Logger::sendINF(char *file_name, __int64 file_size, int FILE_FORMAT){
	time_t t;
	time(&t);
	if(level == LOG_INF){
		std::ostringstream stream;
		stream<<ctime(&t)<<" : "<<"File created! File name "<<file_name<<", file size = "<<file_size<<", file format:";
		if(FILE_FORMAT == ORIGIN_CSV){
			stream<<"TO HANDLE";
		}
		if(FILE_FORMAT == TMP){
			stream<<"TEMPORARY";
		}
		if(FILE_FORMAT == OUT_CSV){
			stream<<"OUT";
		}
		std::string message = stream.str() + "\n";
		if(buff.length() + message.length() < max_bsize){
				buff += message;
			}else{
				out<<buff;
				buff = message;
			}
	}
}

void Logger::sendINF(char *file_name, std::string message){
	time_t t;
	time(&t);
	if(level == LOG_INF){
		std::ostringstream stream;
		stream<<ctime(&t)<<" : "<<"File name : "<<file_name<<" : "<<message;
		std::string message = stream.str();
		if(buff.length() + message.length() < max_bsize){
			buff += message;
		}else{
			out<<buff;
			buff = message;
		}
	}
}
